package cn.ronghuanet.user.controller;

import cn.ronghuanet.user.dto.UserBindWechatDTO;
import cn.ronghuanet.user.dto.UserPhoneRegisterDTO;
import cn.ronghuanet.user.service.IUserService;
import cn.ronghuanet.user.domain.User;
import cn.ronghuanet.user.query.UserQuery;
import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public IUserService userService;


    /**
     * 保存和修改公用的
     * @param user  传递的实体
     * @return Ajaxresult转换结果
     */
    @PostMapping
    public AjaxResult addOrUpdate(@RequestBody User user){
        if( user.getId()!=null)
            userService.update(user);
        else
            userService.insert(user);
        return AjaxResult.success();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult deleteOne(@PathVariable("id") Long id){
        userService.delete(id);
        return AjaxResult.success();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id")Long id)
    {
        User user = userService.selectById(id);
        return AjaxResult.success(user);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        List< User> list = userService.selectAll();
        return AjaxResult.success(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PutMapping
    public AjaxResult queryPage(@RequestBody UserQuery query)
    {
        PageInfo<User> pageList = userService.pageList(query);
        return AjaxResult.success(pageList);

    }

    /**
   * 批量删除
   * @return
   */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        userService.batchDelete(ids);
        return AjaxResult.success();
    }

    @PostMapping("/phone/register")
    public AjaxResult phoneRegister(@Valid @RequestBody UserPhoneRegisterDTO dto){
        userService.phoneRegister(dto);
        return AjaxResult.success();
    }

    @PostMapping("/bind/wechat")
    public AjaxResult bindWechat(@Valid @RequestBody UserBindWechatDTO dto){
        Map<String, Object> map = userService.bindWechat(dto);
        return AjaxResult.success(map);
    }
}
